package com.zmn.oms.business.impl.es;

import com.zmn.base.oms.common.model.es.orderpart.EsOrderPart;
import com.zmn.oms.business.interfaces.es.OrderPartEsBService;
import com.zmn.oms.services.config.ElasticsearchConfig;
import com.zmn.oms.services.interfaces.es.OrderPartESearchService;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.aggregations.Aggregations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类描述：
 *
 * @author wangxiaokun
 * @since 2022/05/23 15:40
 */
@Service
public class OrderPartEsBServiceImpl implements OrderPartEsBService {

    @Autowired
    OrderPartESearchService orderPartESearchService;

    @Autowired
    @Qualifier(ElasticsearchConfig.ES_CLIENT_M2)
    private RestHighLevelClient esClientSecond;

    @Override
    public Aggregations getAggregationsByQuery(NativeSearchQuery nativeSearchQuery) {
        nativeSearchQuery.setTrackTotalHits(true);
        orderPartESearchService.setElasticsearchRestTemplate(esClientSecond);
        return orderPartESearchService.getAggregationsByQuery(nativeSearchQuery);
    }

    @Override
    public List<EsOrderPart> listPageByQuery(NativeSearchQuery nativeSearchQuery) {
        nativeSearchQuery.setTrackTotalHits(true);
        orderPartESearchService.setElasticsearchRestTemplate(esClientSecond);
        return orderPartESearchService.listPageByQuery(nativeSearchQuery);
    }

    @Override
    public Long countByQuery(NativeSearchQuery nativeSearchQuery) {
        orderPartESearchService.setElasticsearchRestTemplate(esClientSecond);
        return orderPartESearchService.countByQuery(nativeSearchQuery);
    }
}
